<?php
use yii\helpers\Html;
$post=\Yii::$app->session->getFlash('post');
?>
<?php $this->beginBlock('title'); ?>
    <?php if(isset($model['game_id'])):?>编辑<?php else:?>新增<?php endif;?>游戏信息
<?php $this->endBlock();?>
<?php $this->beginBlock('breadcrumb'); ?>
<li><a href="<?php echo \Yii::$app->urlManager->createUrl('/game/index')?>">游戏</a></li>
<li class="active"><a href="<?php echo \Yii::$app->urlManager->createUrl('/game/edit-info',['game_id'=>isset($model['game_id'])?$model['game_id']:0]);?>"><?php if(isset($model['game_id'])):?>编辑<?php else:?>新增<?php endif;?>游戏信息</a></li>
<?php $this->endBlock();?>

<link rel="stylesheet" type="text/css" href="<?php echo Yii::$app->request->baseUrl; ?>/js/bootstrap-fileinput-master/css/fileinput.css" />
<style>
.show_upload { display:none; }
</style>
<div class="row">
	<div class="col-sm-12">
		<section class="panel">
			<div class="panel-body">
				<div class="form">
					<form class="cmxform form-horizontal tasi-form" method="post" action="<?php echo \Yii::$app->urlManager->createUrl('/game/save-info');?>">
					   <div class="form-group">
                            <label for="game_name" class="control-label col-lg-2">游戏名称</label>
                            <div class="col-lg-10">
                                <input class="form-control " id="game_name" type="text" disabled="" value="<?=Html::encode($model['game_name']);?>" name="game_name" />
                            </div>
                        </div>
						<div class="form-group">
							<label for="seo_title" class="control-label col-lg-2">SEO标题</label>
							<div class="col-lg-10">
                               	<input class="form-control" id="seo_title" name="seo_title" value="<?php if(isset($post['seo_title'])){ echo $post['seo_title'];}else{ echo Html::encode(isset($model['seo_title'])?$model['seo_title']:''); }?>" type="text" />
                            </div>
						</div>
						<div class="form-group">
							<label for="seo_keyword" class="control-label col-lg-2">SEO关键词</label>
							<div class="col-lg-10">
                               	<input class="form-control" id="seo_keyword" name="seo_keyword" value="<?php if(isset($post['seo_keyword'])){ echo $post['seo_keyword'];}else{ echo Html::encode(isset($model['seo_keyword'])?$model['seo_keyword']:''); }?>" type="text" />
                            </div>
						</div>
						<div class="form-group">
							<label for="seo_description" class="control-label col-lg-2">SEO描述</label>
							<div class="col-lg-10">
                               	<textarea class="form-control " id="seo_description" name="seo_description"><?php if(isset($post['seo_description'])){ echo $post['seo_description'];}else{ echo Html::encode(isset($model['seo_description'])?$model['seo_description']:''); }?></textarea>
                            </div>
						</div>
						<div class="form-group">
							<label for="game_detail" class="control-label col-lg-2">游戏详情</label>
							<div class="col-lg-10">
                               	<textarea class="form-control " id="game_detail" name="game_detail"><?php if(isset($post['game_detail'])){ echo $post['game_detail'];}else{ echo Html::encode(isset($model['game_detail'])?$model['game_detail']:''); }?></textarea>
                            </div>
						</div>
						<div class="form-group">
							<label for="game_album" class="control-label col-lg-2">游戏相册</label>
							<div class="col-lg-10">
                               	<textarea class="form-control " id="game_album" name="game_album"><?php if(isset($post['game_album'])){ echo $post['game_album'];}else{ echo Html::encode(isset($model['game_album'])?$model['game_album']:''); }?></textarea>
                            </div>
						</div>
                        <div class="form-group">
                            <label for="number" class="control-label col-lg-2">游戏Banner</label>
                            <div class="col-lg-10">
                            	<?php if(isset($model['game_banner'])):?>
	                            	<span class="file-input">
	                            		<div class="file-preview">
	                            			<div class="close fileinput-remove">×</div>
	                            			<div class="">
	                            				<div class="file-preview-thumbnails">
													<div data-fileindex="0" class="file-preview-frame">
	   													<img style="width:auto;height:160px;" alt="<?php echo isset($model['game_banner'])?$model['game_banner']:'';?>" title="<?php echo isset($model['game_banner'])?$model['game_banner']:'';?>" class="file-preview-image" src="<?=filepath($model['game_banner']);?>">
	   													<div class="file-thumbnail-footer">
	    													<div style="width: 170px;" class="file-caption-name" title="<?php echo isset($model['game_banner'])?$model['game_banner']:'';?>"><?php echo isset($model['game_banner'])?$model['game_banner']:'';?></div>
														</div>
													</div>
												</div>
												<div class="clearfix"></div>
											</div>
										</div>
									</span>
									<div class="show_upload">
										<input id="file_game_banner" class="file" type="file" name="file" multiple=true>
			                        	<span class="help-block">请上传[jpg、png、gif]格式的图片，建议规格为500*500，不大于1M </span>
									</div>
                            	<?php else:?>
                            		<input id="file_game_banner" class="file" type="file" name="file" multiple=true>
		                        	<span class="help-block">请上传[jpg、png、gif]格式的图片，建议规格为500*500，不大于1M </span>
		                        <?php endif;?>
                            </div>
                        </div>
						<div class="form-group">
                            <label for="basic_information" class="control-label col-lg-2">游戏基本资料</label>
                            <div class="col-lg-10">
                                <textarea class="form-control " id="basic_information" name="basic_information"><?php if(isset($post['basic_information'])){ echo $post['basic_information'];}else{ echo Html::encode(isset($model['basic_information'])?$model['basic_information']:''); }?></textarea>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="m_tongji_code" class="control-label col-lg-2">m统计代码</label>
                            <div class="col-lg-10">
                                <input class="form-control " id="m_tongji_code" type="text" value="<?php if(isset($post['m_tongji_code'])){ echo $post['m_tongji_code'];}else{ echo Html::encode(isset($model['m_tongji_code'])?$model['m_tongji_code']:''); }?>" name="m_tongji_code" />
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="tongji_code" class="control-label col-lg-2">统计代码</label>
                            <div class="col-lg-10">
                                <input class="form-control " id="tongji_code" type="text" value="<?php if(isset($post['tongji_code'])){ echo $post['tongji_code'];}else{ echo Html::encode(isset($model['tongji_code'])?$model['tongji_code']:''); }?>" name="tongji_code" />
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-lg-offset-2 col-lg-10">
                                <input type="hidden" name="_csrf" id="_csrf" value="<?=\Yii::$app->request->csrfToken; ?>" />
        						<?php if(isset($model['game_id'])):?><input type="hidden" name="game_id" value="<?=$model['game_id'];?>" /><?php endif;?>
        						<input type="hidden" value="<?php echo isset($model['game_banner'])?$model['game_banner']:'';?>" id="game_banner" name="game_banner" />
                                <button class="btn btn-success" type="submit"> 保存</button>
                                <button class="btn btn-default" type="button" onclick="javascript:history.go(-1)"> 取消</button>
                            </div>
                        </div>
                    </form>
                </div>
			</div>
		</section>
	</div>
</div>

<?php
$this->beginBlock('script');
?>
<script type="text/javascript" src="<?php echo Yii::$app->request->baseUrl; ?>/js/bootstrap-fileinput-master/js/fileinput.js"></script>
<script type="text/javascript">
//约束上传图片条件
$("#file_game_banner").fileinput({
    allowedFileExtensions : ['jpg', 'png','gif'],
    showPreview : true,
    maxFileSize: 1000,
    minImageWidth: 500,
    minImageHeight: 500,
    showUpload:false,
    //maxFilesNum: 1,
});

$(function() {
	//开始上传图片
    $(document).on('change', "#file_game_banner", function() {
    	var csrfToken = $('meta[name="csrf-token"]').attr("content");
        $.ajaxFileUpload({
            url : "<?php echo \Yii::$app->urlManager->createUrl('/storage/upload')?>",
            secureuri : false,
            fileElementId : 'file_game_banner',
            dataType : 'json',
            data : { _csrf:csrfToken },
            success : function (data, status) {
                $("#game_banner").val(data.path);
            },
            error : function (data, status, e) {
                alert(e);
            }
        });
    });
});

//修改图片时，关闭图片显示改为上传
$(".fileinput-remove").click(function(){
	$(".file-input").toggle();
	$("#game_banner").val("");
	$(".show_upload").show();
	$(".file-input-new").show();
});

//产生下级游戏类型
$(document).on('change', ".sub_type", function() {
    var obj=$(this);
    var parent_id = $(this).val();
    $(this).nextAll(':not(button)').remove();
    $.getJSON("<?=\Yii::$app->urlManager->createUrl('game-type/sub-types');?>", { parent_id:parent_id }, function(data) {
        if (data.length > 0) {
            var select = '&nbsp;&nbsp;<select class="form-control sub_type" style="width:25%;display:inline;" name="type[]" tabindex="1"><option value="0">--请选择--</option>';
            $(data).each(function(i, e) {
                select += "<option value='"+ e.type_id+"'>"+ e.type_name+"</option> ";
            });
            select += "</select>";
            obj.after(select);
        }
    });
});

//增加一行
$(document).on('click', '.addSelecter', function(){
    addLinkSelecter();
});

//删除一行
$(document).on('click', '.removeSelecter', function()
{
    var parent = $(this).parent();
    var del = false;
    if (parent.find('[name="cust[]"]').val() != '') {
        if (confirm('确定要删除吗？')) {
            del = true;
        }
    } else {
        del = true;
    }
    if (del) {
        $(this).parent().remove();
        $("#link_list .addSelecter:last").show();
        if ($("#link_list .removeSelecter").size() <= 1 ) {
            $("#link_list .removeSelecter").hide();
        }
    }
});

//克隆函数
function addLinkSelecter()
{
    $("#link_list .addSelecter").hide();
    var link = $("#linkSelecter").clone().show();
    $("#link_list").append(link);
    $("#link_list .removeSelecter").show();
}

$("#link_list .addSelecter:not(':last')").hide();
</script>
<?php $this->endBlock('script');?>